<?php
	/**
	 * Created by JetBrains PhpStorm.
	 * User: alex
	 * Date: 23.07.13
	 * Time: 11:57
	 * To change this template use File | Settings | File Templates.
	 */
	function getContentNigma($url)
	{
		$curl = curl_init();
		curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($curl, CURLOPT_TIMEOUT, 600);
		curl_setopt($curl, CURLOPT_USERAGENT,  "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.72 Safari/537.36");
		curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
		curl_setopt($curl, CURLOPT_REFERER, 'https://www.google.com.ua');
		curl_setopt($curl, CURLOPT_URL, "$url");
		$content = curl_exec($curl);
		preg_match('`<ol id="reslist">(.*?)</ol`s', $content, $text);
		preg_match('`pageNext.*?>(.*?)page_ctrl`s', $content, $hrefContainer);


		preg_match_all('`href="(.*?)"`', $hrefContainer[1], $href);

		$cleanText = str_replace("<b>", "", $text[1]);
		return array("text" => $cleanText, "link" => $href[1]);
	}
	function getContentYandex($url)
	{
		$curl = curl_init();
		curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($curl, CURLOPT_TIMEOUT, 600);
		curl_setopt($curl, CURLOPT_USERAGENT,  "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.72 Safari/537.36");
		curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
		curl_setopt($curl, CURLOPT_REFERER, 'https://www.google.com.ua');
		curl_setopt($curl, CURLOPT_URL, "$url");
		$content = curl_exec($curl);
		echo $content;
		preg_match('`<ol>(.*?)</ol>`', $content, $text);
		var_dump($text);
		preg_match('`b-pager__pages.*?>(.*?)</div>`', $content, $hrefContainer);


		preg_match_all('`href="(.*?)"`', $hrefContainer[1], $href);

		$cleanText = str_replace("<b>", "", $text[1]);
		var_dump($cleanText);
		return array("text" => $cleanText, "link" => $href[1]);
	}
	function getContentGoogle($url)
	{
		$curl = curl_init();
		curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($curl, CURLOPT_TIMEOUT, 600);
		curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.72 Safari/537.36');
		curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
		curl_setopt($curl, CURLOPT_REFERER, 'http://rozetka.ua/');
		curl_setopt($curl, CURLOPT_PROXY, "74.221.210.119:7808");
		curl_setopt($curl, CURLOPT_URL, "$url");

		$content = curl_exec($curl);
		echo $content;
		preg_match('`center_col">(.*?)(?:<div id="foot">)`', $content, $text);
		preg_match('`id="nav">(.*?)</table>`', $content, $hrefContainer);

		preg_match_all('`href="(.*?)"`', $hrefContainer[1], $href);

		$cleanText = str_replace("<b>", "", $text[1]);
		echo $cleanText;
		return array("text" => $cleanText, "link" => $href[1]);
	}

	function getMail($text)
	{
		preg_match_all('`(\w*?@.*?) `', $text, $mail);
		$newMail = array_unique($mail[1]);
		foreach($newMail as $email)
		{
			if(filter_var($email, FILTER_VALIDATE_EMAIL))
			{
			saveEmail($email);
			}
		}
	}
	function saveEmail($email, $name ="")
	{
		echo $email;
		$dbhost = "localhost"; // Имя хоста БД
		$dbusername = "root"; // Пользователь БД
		$dbpass = ""; // Пароль к базе
		$dbname = "email";

		$dbconnect = @mysql_connect ($dbhost, $dbusername, $dbpass);
		if (!$dbconnect) { echo ("Не могу подключиться к серверу базы данных!"); }
		if(@mysql_select_db($dbname)) { echo "Подключение к базе $dbname установлено!"; }
		else die ("Не могу подключиться к базе данных $dbname!");
		$sql = <<<SQL
INSERT IGNORE INTO
				email
					(email, name)
VALUES
					($email, $name);
SQL;
		mysql_query($sql);
	}

	$parsContainer = getContentGoogle("https://www.google.com.ua/search?q=ukr.net+site%3Arabota.ua&oq=ukr.net+site%3Arabota.ua&aqs=chrome.0.69i59j69i61j69i62.16342j0&sourceid=chrome&ie=UTF-8");
//	$parsContainer = getContentYandex("http://yandex.ua/yandsearch?text=ukr.net&site=rabota.ua&lr=143");
//	$parsContainer = getContentNigma("http://www.nigma.ru/?s=ukr.net+site%3Arabota.ua");
	getMail($parsContainer["text"]);
//	foreach($parsContainer["link"] as $link)
//	{
//		$parsContainer = getContentNigma("https://www.google.com.ua/".$link);
//		getMail($parsContainer["text"]);
//	}

